//
//  DSLeaderboardController.m
//  DSJY
//
//  Created by 冯华强 on 16/9/24.
//  Copyright © 2016年 dsjy. All rights reserved.
//  排行榜

#import "DSLeaderboardController.h"
#import "DSLeaderboardCell.h"
#import "DSLearnInfoViewModel.h"
#import "DSLeaderboardViewModel.h"
#import "MTConst.h"
#import "DSLearnStatis.h"
#import "DSAccountViewModel.h"

static NSString *identifier = @"LeaderBoardCell";
@interface DSLeaderboardController ()<UITableViewDelegate, UITableViewDataSource>
#pragma mark - 拖线属性
//tableView
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//亲爱的xiaomai,您好！
@property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//属性
@property (nonatomic, strong) DSLeaderboardViewModel *leaderboardVM;
@end

@implementation DSLeaderboardController

#pragma mark - 懒加载
- (DSLeaderboardViewModel *)leaderboardVM
{
    if (_leaderboardVM == nil) {
        _leaderboardVM = [[DSLeaderboardViewModel alloc] init];
    }
    return _leaderboardVM;
}

#pragma mark - 系统回调
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置UI
    [self setupUI];
    //显示backItem
    [self setupnavigationItem];
    //绑定VM
    [self bindViewModel];
    //绑定事件
    [self bindEvent];
}

#pragma mark - 设置UI
- (void)setupUI
{
    self.welcomeLabel.text = [NSString stringWithFormat:@"亲爱的%@，您好！", [DSAccountViewModel sharedAccount].account.name];
}

- (void)setupnavigationItem
{
    self.showBackItem = YES;
}

- (void)bindViewModel
{
    @weakify(self);
    self.tableView.allowsSelection = NO;
    //加载上拉下拉
    self.tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self checkNetWorkWithRefreshType:YQRefreshTypeHeader];
    }];
    
//    self.tableView.mj_footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
//        @strongify(self);
//        [self checkNetWorkWithRefreshType:YQRefreshTypeFooter];
//    }];
    //默认先隐藏footer
//    self.tableView.mj_footer.hidden = NO;
}

#pragma mark - 设置事件
- (void)bindEvent
{
    @weakify(self);
    //如果验证通过，调用命令信号
    [self.leaderboardVM.requestCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        [self reloadDataWithTableView:self.tableView currentPage:self.leaderboardVM.pageNum totalPage:self.leaderboardVM.response.total_page];
        //给标题赋值
        DSLearnStatis *myLearnStatis = self.leaderboardVM.myLearnStatis;
        if ([DSAccountViewModel sharedAccount].account.isSharescore) {
            if (myLearnStatis != nil) {
                self.titleLabel.text = [NSString stringWithFormat:@"您目前全国排名第%ld名，总得分%@分，学习总时间是%@。", (long)myLearnStatis.paiming, myLearnStatis.totalscore, myLearnStatis.totalusetime];
            }
        }else{
            self.titleLabel.text = @"您已关闭将成绩分享到排行榜显示，可以在首页设置中打开!";
        }
    }];
    
    //有网络
    [self.networkWiFiWWANSignal subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        // 执行请求
        [self.leaderboardVM.requestCommand execute:tuple[0]];
    }];
    
    //无网络
    [self.networkUnknownNotReachableSignal subscribeNext:^(id x) {
        @strongify(self);
        [self showEmptyHUDWithView:self.tableView];
    }];
    
    //执行请求
    [self checkNetWorkWithRefreshType:YQRefreshTypeHeader];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.leaderboardVM.learnInfoVMList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DSLeaderboardCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    DSLearnInfoViewModel *learnInfoVM = self.leaderboardVM.learnInfoVMList[indexPath.row];
    learnInfoVM.indexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
    cell.learnInfoVM = learnInfoVM;
    return cell;
}

#pragma mark - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return XCYTableHead0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return XCYTableHead0;
}

@end
